package com.cskaoyan.wordmemorize.controller.app;

import cn.dev33.satoken.stp.StpUtil;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import com.cskaoyan.wordmemorize.service.UserCheckinService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "移动端-打卡模块")
@RestController
@RequestMapping("/user/checkin")
public class UserCheckinController {
    @Resource
    UserCheckinService userCheckinService;

    // APP端下载打卡数据(如果分页参数没有传的话，就返回所有打卡数据)
    @GetMapping("/list")
    public Result<PageDTO<UserCheckinDTO>> getCheckInData(PageRequest pageRequest,Long userId){
        try {
            PageDTO<UserCheckinDTO> checkInData = userCheckinService.getCheckInData(pageRequest, userId);
            return Result.ok(checkInData);
        } catch (Exception e) {
           return Result.fail();
        }
    }
    // APP端获取用户打卡数据
    @GetMapping("/statistics")
    public Result<UserCheckinStatisticsDTO> getUserCheckinStatistics(){
        try {
            Long userId = StpKit.USER.getLoginIdAsLong();
            UserCheckinStatisticsDTO userCheckinStatistics = userCheckinService.getUserCheckinStatistics(userId);
            return Result.ok(userCheckinStatistics);
        } catch (Exception e) {
            return  Result.fail();
        }
    }
    // 打卡
    @PostMapping()
    public Result<UserCheckinStatisticsDTO> checkin(@RequestBody UserCheckinCommand command){
        Long userId = StpKit.USER.getLoginIdAsLong();
        command.setUserId(userId.toString());
        UserCheckinStatisticsDTO checkin = userCheckinService.checkin(command);
        return Result.ok(checkin);
    }
}
